<?php

$db = new SQLiteDatabase('InstantInput.db');

$course = sqlite_escape_string($_GET['course']);
$title = sqlite_escape_string($_GET['title']);
$startTime = sqlite_escape_string($_GET['startTime']);
$endTime = sqlite_escape_string($_GET['endTime']);
$days = sqlite_escape_string($_GET['days']);
$location = sqlite_escape_string($_GET['location']);

$db->query("Insert INTO Classes (course, title, start_time, end_time, days, location) VALUES ('" . $course . "', '" . $title . "', '" . $startTime . "', '" . $endTime . "', '" . $days . "', '" . $location . "')");

$result = $db->query("SELECT class_id FROM Classes ORDER BY class_id DESC LIMIT 1");

$new_class = $result->current();  

$db->queryExec("Insert INTO User_Classes (sunet_id, class_id) VALUES ('" . $_ENV["WEBAUTH_USER"] . "', " . $new_class['class_id'] . ")");

$classes = array();

$result = $db->query("SELECT * FROM Classes WHERE Classes.class_id IN (SELECT User_Classes.class_id FROM User_Classes WHERE User_Classes.sunet_id = '" . $_ENV["WEBAUTH_USER"] . "')");

while ($result->valid()) 
{
	$curr = $result->current();

	$class = array(
		"uniqueCourseNumber"=> $curr['class_id'],
		"course"=>$curr['course'],
		"title"=>$curr['title'],
		"startTime"=>$curr['start_time'],
		"endTime"=>$curr['end_time'],
		"days"=>array("F"),
		"location"=>$curr['location']
	);
	
	$classes[] = $class;
	$result->next();	
}	

print json_encode($classes);

?>